import { _decorator, Component, Label, Node } from 'cc';
import { UIManager } from '../common/UIManager';
import { Task, TaskStatus, TaskType } from '../entity/Task';
import { PrefabEnum } from '../common/PrefabEnum';
import { EventManager } from '../event/EventManager';
const { ccclass, property } = _decorator;

@ccclass('TaskTip')
export class TaskTip extends Component {
    @property(Label)
    taskName: Label
    @property(Node)
    redDot:Node

    async start() {

        this.node.on(Node.EventType.TOUCH_END, this.onTouch, this)
        EventManager.instance.on(EventManager.EventType.ReceiveTaskBonus, this.init, this)
        EventManager.instance.on(EventManager.EventType.GetVillageDetail, this.init, this)
    }

    async init(){
        await UIManager.instance.showLoading()
        Task.allTaskList = []
        let list = await Task.getTaskListByType(TaskType.Main)
        UIManager.instance.closeLoading()

        for(let item of list){
            if(item.status == TaskStatus.Complete){
                this.taskName.string = item.name
                return
            }
        }

        for(let item of list){
            if(item.status == TaskStatus.NoComplete){
                this.taskName.string = item.name
                this.redDot.active = false
                return
            }
        }

       
    }

    onTouch(){
        UIManager.instance.openUI(PrefabEnum.TaskIndex)
    }


}


